package com.dome.common.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

/**
 * @param
 * @author 汤顺凯
 */
public class DateUtlis {

    /**
     * 获取当前系统时间 精确到时分秒
     * @return
     */
    public static String getDateTime(){
        TimeZone timeZoneSH = TimeZone.getTimeZone("Asia/Shanghai");
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        simpleDateFormat.setTimeZone(timeZoneSH);
        String dateTime = simpleDateFormat.format(date);
        return dateTime;
    }

    /**
     * 获取当前系统时间，到日期
     * @return
     */
    public static String getDate(){
        TimeZone timeZoneSH = TimeZone.getTimeZone("Asia/Shanghai");
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
        simpleDateFormat.setTimeZone(timeZoneSH);
        String dates = simpleDateFormat.format(date);
        return dates;
    }

    /**
     * 对时间进行比较 用于判断设备到期时间
     * @param prior  开始时间
     * @param behind  结束时间
     * @return
     * @throws ParseException
     */
    public static Boolean compareDate(String prior,String behind) throws ParseException {
        SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy-MM-dd");
        Date priors =simpleDateFormat.parse(prior);
        Date behinds =simpleDateFormat.parse(behind);
        return priors.getTime() < behinds.getTime();
    }

    /**
     * 对时间的对比精确到时分秒
     * @param prior 开始时间
     * @param behind 结束时间
     * @return
     * @throws ParseException
     */
    public static Boolean compareDateTime(String prior,String behind) throws ParseException {
        SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date priors =simpleDateFormat.parse(prior);
        Date behinds =simpleDateFormat.parse(behind);
        return priors.getTime() < behinds.getTime();
    }
    /**
     * 字符串转时间 yyyy-MM-dd HH:mm
     * @param bDate
     * @return
     * @throws ParseException
     */
    public static Date StringToDateHHMM(String bDate) throws ParseException {
        DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date bdate = format1.parse(bDate);
        return bdate;
    }

    /**
     * 字符串转时间 yyyy-MM-dd
     * @param bDate
     * @return
     * @throws ParseException
     */
    public static Date StringToDateDD(String bDate) throws ParseException {
        DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
        Date bdate = format1.parse(bDate);
        return bdate;
    }

    /**
     * 时间类型转String yyyy-MM-dd
     * @param date
     * @return
     * @throws ParseException
     */
    public static String DateToString(Date date){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
        String dates = simpleDateFormat.format(date);
        return dates;
    }

    /**
     * 判断是否是周末
     * @param bDate
     * @return
     * @throws ParseException
     */
    public static Boolean isWeekend(String bDate) throws ParseException {
        DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date bdate = format1.parse(bDate);
        Calendar cal = Calendar.getInstance();
        cal.setTime(bdate);
        if(cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY){
            return true;
        } else{
            return false;
        }
    }
}
